<?php defined('BASEPATH') or exit('No direct script access allowed');

class Escort_zone_m extends MY_Model {
	var $msgLength = 250;
	
	function __construct(){
		parent::__construct();
	}
	
	function submit_escort_send_message(){
		$escort_id = $this->input->post('escort_id', TRUE);
		$message = substr( $this->input->post('message_textarea', TRUE) , 0, $this->msgLength );
		$ESCORT = $this->escort_entity->init('id',$escort_id);
		$USER = $this->user_entity->init('id',$ESCORT->getUserId());
		
		$current_dbprefix = $this->db->dbprefix;
		$this->db->set_dbprefix('default_');
		
		$this->db->set('origin', USER_TYPE_ESCORT );
		$this->db->set('escort_id', $escort_id );
		$this->db->set('owner', $USER->getUserName() );
		$this->db->set('solver_permission_slug', USER_SUPPORT_LEVEL_1 );
		$this->db->set('created_time', time() );
		$this->db->set('status', 'pending' );
		$this->db->insert(TBL_MESSAGE_THREAD);
		
		$thread_id =  $this->db->insert_id();
		
		$this->db->set('sender', $USER->getUserName());
		$this->db->set('sent_time', time());
		$this->db->set('message', $message);
		$this->db->set('thread_id', $thread_id );
		$this->db->set('read', 1);
		$this->db->insert(TBL_MESSAGE);
		
		$this->db->set_dbprefix($current_dbprefix);
		
		$this->action_log->insertNewAction($USER->getId(),'log_action_send_message');
		
		$countUnread = $this->escort_message_thread_entity->countAllUnreadMessagesOfUser($ESCORT->getId(),USER_TYPE_ESCORT);	
		$countUnreadOfThread = $this->escort_message_thread_entity->countAllUnReadMessage($thread_id);
		echo json_encode(
							array(
									'thread_id'=>$thread_id, 
									'unreadMsg'=>$countUnread,
									'unreadMsgThread'=>$countUnreadOfThread,
									'result'=>'ok',
                                    'msg'=>language_translate('contact_zone_successfully_message')
								)
						);
		//echo 'ok';
		exit();
	}
	
	function submit_send_message_reply_thread(){
		extract($this->input->post());
		$message = substr( $message_textarea , 0, $this->msgLength );
	 	
		$current_dbprefix = $this->db->dbprefix;
		$this->db->set_dbprefix('default_');
		
		$this->db->set('sender', $username);
		$this->db->set('sent_time', time());
		$this->db->set('message', $message);
		$this->db->set('thread_id', $thread_id );
		$this->db->set('read', 1);
		$this->db->insert(TBL_MESSAGE);
		
		$this->db->set_dbprefix($current_dbprefix);
		
		$USER = getAccountUserObject();
		$ESCORT = $this->escort_entity->init('user_id',$USER->getId());
		
		$this->action_log->insertNewAction($USER->getId(),'log_action_reply_message');
		
		$countUnread = $this->escort_message_thread_entity->countAllUnreadMessagesOfUser($ESCORT->getId(),USER_TYPE_ESCORT);	
		$countUnreadOfThread = $this->escort_message_thread_entity->countAllUnReadMessage($thread_id);
		echo json_encode(
							array(
									'thread_id'=>$thread_id, 
									'unreadMsg'=>$countUnread,
									'unreadMsgThread'=>$this->escort_message_thread_entity->displayUnreadMessage($thread_id),
									'result'=>'ok'
								)
						);
		//echo 'ok';
		exit();
	}
	
	function toggleReadUnreadMessage(){
		extract($this->input->post());
		$messageObj = $this->escort_message_thread_entity->getMessageObject($message_id);
		
		$USER = getAccountUserObject();
		$ESCORT = $this->escort_entity->init('user_id',$USER->getId());
		
		if($messageObj){
			if($messageObj->read == 1){
				$this->db->set('read', 0);
				$this->db->where('id',$message_id)->update(TBL_MESSAGE);
				$cssClass = 'unread';
			}else{
				$this->db->set('read', 1);
				$this->db->where('id',$message_id)->update(TBL_MESSAGE);
				$cssClass = 'read';
			}	
			$countUnread = $this->escort_message_thread_entity->countAllUnreadMessagesOfUser($ESCORT->getId(),USER_TYPE_ESCORT);	
			//$countUnreadOfThread = $this->escort_message_thread_entity->countAllUnReadMessage($messageObj->thread_id);
			echo json_encode(
								array(
									'thread_id'=>$messageObj->thread_id, 
									'css'=>$cssClass, 'unreadMsg'=>$countUnread,
									'unreadMsgThread'=>$this->escort_message_thread_entity->displayUnreadMessage($messageObj->thread_id)
									)
							);
			exit();
		}
	}
	
	function deleteMessage(){
		extract($this->input->post());
		$USER = getAccountUserObject();
		$ESCORT = $this->escort_entity->init('user_id',$USER->getId());
		$messageObj = $this->escort_message_thread_entity->getMessageObject($message_id);
		
		$this->action_log->insertNewAction($USER->getId(),'log_action_delete_message');
		$this->db->where('id',$message_id)->update(TBL_MESSAGE,array('is_deleted'=>1));
		
		$countUnread = $this->escort_message_thread_entity->countAllUnreadMessagesOfUser($ESCORT->getId(),USER_TYPE_ESCORT);	
		//$countUnreadOfThread = $this->escort_message_thread_entity->countAllUnReadMessage($messageObj->thread_id);
			
		echo json_encode(array('unreadMsg'=>$countUnread,
							   'unreadMsgThread'=>$this->escort_message_thread_entity->displayUnreadMessage($messageObj->thread_id),
							   'thread_id'=>$messageObj->thread_id	
						));
		exit();
	}
	
	
	
	
	
	
	
	
	
	
//end class
}	